#include "mylogin.h"
#include "ui_mylogin.h"

Mylogin::Mylogin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Mylogin)
{
    ui->setupUi(this);

    connect(ui->pushButton_Search,SIGNAL(clicked()),this,SLOT(btnselectSlot()));

    udpsocket=new QUdpSocket(this);
    socket=new QTcpSocket(this);
    timer = new QTimer(this);
    connect(ui->pushButton_Connect,SIGNAL(clicked()),this,SLOT(btnconnectSlot()));

}

Mylogin::~Mylogin()
{
    if(socket->isOpen())
    {
        socket->close();
    }
    delete ui;
}

void Mylogin::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap map(":/new/prefix1/res/1.JPG");
    painter.drawPixmap(0,0,this->width(),this->height(),map);
}

QString Mylogin::inJSON(QByteArray byteArray)
{
    //解析JSON
    QString strName;
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError);
    if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
    {
        if (doucment.isObject())
        {
            QJsonObject object = doucment.object();
            if (object.contains("up_config"))
            {
                QJsonValue value = object.value("up_config");
                if (value.isString())
                {
                    strName = value.toString();
                    qDebug() << strName;
                }
            }
        }
    }
    return strName;
}

void Mylogin::putJson()
{
    QString array;
    readPath="F:/QTPro/edge-computing/config/config.json";
    QFile readFile(readPath);
    readFile.open(QIODevice::ReadOnly);
    while(!readFile.atEnd())    // 判断是否读到文件尾部
    {
        array = readFile.read(2048);    // 每次读取2kb
        QByteArray bytearray=array.toUtf8();
        socket->write(bytearray);
    }
    qDebug()<<"传输完成";
}

void Mylogin::btnselectSlot()
{
    QByteArray text="114514";
    QHostAddress broadaddr("192.168.31.140");
    int port=8886;
    udpsocket->writeDatagram(text,broadaddr,port);
    connect(udpsocket,SIGNAL(readyRead()),this,SLOT(readReadySlot()));
}

void Mylogin::btnconnectSlot()
{
    QMessageBox::information(this,"information","连接设备成功");
    socket->connectToHost(sender.toString(),senderPort);

    connect(socket,SIGNAL(connected()),this,SLOT(connectSlot()));
}

void Mylogin::readReadySlot()
{
    datagram.resize(udpsocket->pendingDatagramSize());

    // 接收数据
    udpsocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    qDebug() << "Received message from " << sender.toString() << ": " << senderPort << " : " << datagram.data();
    QString ip=sender.toString().append(": %1");
    ui->lineEdit->setText(ip.arg(senderPort));
    emit getConnectIp(sender.toString());
}

void Mylogin::connectSlot()
{
    QJsonObject json;
    json.insert("type",1);
    // 构建 Json 文档
    QJsonDocument document;
    document.setObject(json);
    QByteArray byteArray = document.toJson(QJsonDocument::Compact);
    QString jtext(byteArray);
    socket->write(byteArray);
    qDebug()<<jtext;
    connect(socket,SIGNAL(readyRead()),this,SLOT(TCPreadReadySlot()));
}

void Mylogin::TCPreadReadySlot()
{
    QByteArray byteArray = socket->readAll();
    QString file=inJSON(byteArray);
    QString trues="true";
    int refult=file.compare(trues);
    if(refult==0)
    {
        qDebug()<<"文件不存在";
        putJson();//下发JSON文件
    }

}
